<?php


namespace app\api\controller;


use think\facade\Db;

class Test
{
    protected $title = '销售明细';
    protected $table = 'sale_contract_detail';
    protected $sType=1;
    public function index()
    {
        $param = request()->param();
            $where[] = ['scd.status', '=', 4];
            $where[] = ['scd.type', '=', $this->sType];
            $where[] = ['scd.lockStatus', '=', 1];

            if (isset($this->param['status']) && $this->param['status']) {
                $where[] = ['scd.status', '=', $this->param['status']];
            }
            if (isset($this->param['buyType']) && $this->param['buyType']) {//采购状态
                $where[] = ['scd.buyStatus', '=', $this->param['buyType']];
            }
            if (isset($this->param['outStatus']) && $this->param['outStatus']) {//出库状态
                $where[] = ['scd.outStatus', '=', $this->param['outStatus']];
            }
            if (isset($this->param['moneyStatus']) && $this->param['moneyStatus']) {//回款状态
                $where[] = ['sc.payStatus', '=', $this->param['moneyStatus']];
            }
            if (isset($this->param['stokType']) && $this->param['stokType']) {//锁定状态
                $where[] = ['scd.lockStatus', '=', $this->param['stokType']];
            }
            if (isset($this->param['name']) && $this->param['name']) {
                $where[] = ['py.name', 'like', '%' . $this->param['name'] . '%'];
            }
            if (isset($this->param['saleUnit']) && $this->param['saleUnit']) {
                $where[] = ['d.title', 'like', '%' . $this->param['saleUnit'] . '%'];
            }
            if (isset($this->param['saller']) && $this->param['saller']) {
                $where[] = ['ad.name', 'like', '%' . $this->param['saller'] . '%'];
            }
            if (isset($this->param['orderNum']) && $this->param['orderNum']) {
                $where[] = ['sc.orderNum', 'like', '%' . $this->param['orderNum'] . '%'];
            }
            if (isset($this->param['orderDate']) && $this->param['orderDate']) {
                $dates = explode('~', $this->param['orderDate']);
                $where[] = ['sc.orderDate', 'between', $dates];
            }
            if (isset($this->param['specs']) && $this->param['specs']) {
                $where[] = ['md.specs', 'like', '%' . $this->param['specs'] . '%'];
            }
            if (isset($this->param['buyCode']) && $this->param['buyCode']) {
                $where[] = ['md.buyCode', 'like', '%' . $this->param['buyCode'] . '%'];
            }
//            $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
            $list = Db::name($this->table)
                ->alias('scd')
                ->leftJoin('sale_contract sc', 'sc.id=scd.orderId')
                ->leftJoin('purveyor py', 'py.id=sc.punterId')
                ->leftJoin('department d', 'd.id=sc.salesUnitId')
                ->leftJoin('admin ad', 'ad.id=sc.saleId')
                ->leftJoin('material_dossier md', 'md.id=scd.mdId')
                ->where($where)
                ->field('scd.*,py.name as pName,d.title as dName,ad.name as saleName,md.bar_code,md.name as mdName
                ,md.brand,md.specs,md.buyCode,md.unit,sc.orderDate,sc.orderNum as orderNum')
                ->order('scd.id DESC')
//                ->paginate($rows)
                ->select()
                ->each(function ($item) {
//                    $buyContractId=Db::name('sale_contract_detail')->where('status',4)->where('saleContractDetailId',$item['id'])
//                        ->column('orderId');
//                    $item['cgcb'] = Db::name('sale_contract')->where('status','4')->where('id','in',$buyContractId)->sum('countMoney');
//                    $item['cgcb'] = Db::name('sale_contract_detail')->where('type',2)->where('status','4')
//                        ->where('saleContractDetailId','=',$item['id'])->where('mdId',$item['mdId'])->sum('bjMoney');
//                    $item['cgcb']=$item['cgcb']?$item['cgcb']:0;

//                    $item['hsml'] = $item['bjMoney']-$item['cgcb'];
//                    $item['msmll']=0;
//                    if ($item['hsml']){
//                        $item['msmll'] = round( $item['hsml']/$item['bjMoney']*100,2);
//                    }

//                    $item['cgNum'] = Db::name('sale_contract_detail')
//                        ->where('status', '=', 4)
//                        ->where('saleContractDetailId', $item['id'])
//                        ->where('type', '=', 2)
//                        ->sum('bjNum');
                    $item['lockedNum'] = Db::name('sale_contract_lock')
                        ->where('status', '>', -1)
                        ->where('detailId', $item['id'])->sum('lockNum');
//                    $buyContractDId=Db::name('sale_contract_detail')->where('status',4)
//                        ->where('type',2)->where('saleContractDetailId',$item['id'])->column('id');
//                    $item['inNum'] = Db::name('stock_order_detail')
//                        ->where('status', '=', 2)
//                        ->where('saleContractDetailId', "in", $buyContractDId)
//                        ->where('mdId', $item['mdId'])
//                        ->where('type', '=',3)
//                        ->sum('num');
//                    $item['outNum'] = Db::name('stock_order_detail')
//                        ->where('status', '=', 2)
//                        ->where('saleContractDetailId', $item['id'])
//                        ->where('mdId', $item['mdId'])
//                        ->where('type', '=', 1)
//                        ->sum('num');
//                    $item['refundNum'] = Db::name('stock_order_detail')
//                        ->where('status', '=', 2)
//                        ->where('saleContractDetailId', $item['id'])
//                        ->where('mdId', $item['mdId'])
//                        ->where('type', '=', 2)
//                        ->sum('num');
//                    $item['color']='';
//                    if ($item['bjNum']==$item['inNum']){
//                        $item['color']='background:#e7c609';
//                    }
                    return $item;
                });
            $ids=[];
            foreach ($list as $v){
                if ($v['lockedNum']!=0&&$v['cgNum']<$v['bjNum']){
                    $ids[]=$v['id'];
                }
            }
            halt(['ids'=>$ids,
            'list'=>$list]);
            Db::name($this->table)->where('id','in',$ids)
                ->update(['lockStatus'=>2]);
            halt(123);

    }
}